package com.tanhua.server.controller;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 手机号登录-发送短信验证码
     * POST /user/login
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        return userService.login(phone);
    }

    /**
     * 手机号登录-校验验证码
     * POST/user/loginVerification
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        return userService.loginVerification(phone, code);
    }

    /**
     * 手机号登录-完善用户信息
     * POST/user/loginReginfo
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {
        return userService.saveUserInfo(userInfo);
    }

    /**
     * 手机号登录-完善用户信息-上传头像
     * POST /user/loginReginfo/head
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity saveAvatar(MultipartFile headPhoto) throws IOException {
        return userService.saveAvatar(headPhoto);
    }
}
